﻿using System;
using ExplorerService.Common.DataContracts.Operations;
using ExplorerService.Common.ServiceContracts;
using ExplorerService.Managers;

namespace ExplorerService.Communication
{
    public class ClientOperationService : ClientSessionMonitoredService, IClientOperationService
    {
        /// <summary>
        /// Method called to clean up service resources
        /// </summary>
        protected override void ServiceDispose()
        {
        }

        /// <summary>
        /// Starts the operation session by registering with the session manager
        /// </summary>
        /// <param name="sessionId"></param>
        public void StartOperationSession(Guid sessionId)
        {
            VerifySessionIdAndRegister(sessionId);
        }

        /// <summary>
        /// Cancels an operation
        /// </summary>
        /// <param name="operationId"></param>
        public void CancelOperation(Guid operationId)
        {
            OperationOrchestrator.Instance.CancelOperation(operationId);
        }

        /// <summary>
        /// Posts a new operation to be performed by an application
        /// </summary>
        public bool PostOperation(Operation operation)
        {
            if (operation == null) return false;
            if (operation.ApplicationSessionId == Guid.Empty) return false;
            operation.ClientSessionId = SessionId;
            return OperationOrchestrator.Instance.QueueOperationForApplication(operation);
        }

        /// <summary>
        /// Ends the current operation session
        /// </summary>
        public void EndOperationSession()
        {
            //Handled by the dispose in base
        }
    }
}
